//
//  HSLeftViewController.swift
//  Home_School_08
//
//  Created by 张晓林 on 2016/12/23.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit
import SnapKit

class HSLeftViewController: UIViewController {
    
    @IBOutlet var leftBtnArray: [UIButton]! // 左边按钮数组
    
    @IBOutlet weak var leftIconV: UIImageView! // 头像
    @IBOutlet weak var leftTitleL: UILabel! // 姓名标题
    @IBOutlet weak var leftAccountL: UILabel! // 账号
    @IBOutlet weak var leftClassNameL: UILabel! // 班级
    @IBOutlet weak var leftSchoolNameL: UILabel! // 学校
    @IBOutlet weak var leftScoreL: UILabel! // 表现分
    @IBOutlet weak var leftScoreBgV: UIView! // 分数视图背景
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 
        setupUI()
    }
    
    @IBAction func leftBtnClicked(_ sender: UIButton) {
        // 获得当前按钮的标题
        let btnTitle = sender.currentTitle ?? ""
        switch btnTitle {
        case "课程表":
            // 跳转到课表
            present(HSClassTableController(), animated: true, completion: nil)
            
        case "关于我们":
            print("关于我们")
        case "意见反馈":
            print("意见反馈")
        default:
            print("退出")
            // 从storyboard中加载登录视图
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let loginVc = sb.instantiateViewController(withIdentifier: "HSLoginController")
            UIApplication.shared.keyWindow?.rootViewController = loginVc
        }
    }
    

    func setupUI() {
        // 设置头像圆角
        leftIconV.layer.cornerRadius = 40
        leftIconV.layer.masksToBounds = true
        leftScoreBgV.backgroundColor = UIColor.clear
        // 设置左边按钮的高亮背景图片
        for btn in leftBtnArray {
            // 设置高亮背景图片
            btn.setBackgroundImage(UIImage.image(withColor: UIColor(white: 0.9, alpha: 1.0)), for: .highlighted)
        }
        
        // 赋值信息参数
        // 获得图片的加密字符串
        if let base64Str = userInfo["studentphoto"] as? String,
            // 将加密字符串转换我二进制
            let imgData = Data(base64Encoded: base64Str, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters) {

            leftIconV.image = UIImage(data: imgData)
        }
        
        let studentName = userInfo["studentName"] as? String
        leftTitleL.text = "\(studentName ?? "") 家长"
        leftAccountL.text = userInfo["studentNum"] as? String
        leftClassNameL.text = userInfo["classname"] as? String
        leftSchoolNameL.text = userInfo["schoolname"] as? String
        // 表现分
        leftScoreL.text = userInfo["studentScore"] as? String
        
        let score = Int(leftScoreL.text ?? "") ?? 0
        var scoreRank = 0
        
        switch score {
        case 0..<10:
            scoreRank = 0
        case 10..<50:
            scoreRank = 1
        case 50..<150:
            scoreRank = 2
        case 150..<350:
            scoreRank = 3
        case 350..<850:
            scoreRank = 4
        default:
            scoreRank = 5
        }
        // 循环添加图片
        for i in 1...5 {
            // 初始化图片视图
            var imageName = ""
            if i <= scoreRank {
                imageName = "jujube\(i)_light"
            }else {
                imageName = "jujube\(i)_dark"
            }
            
            let imageV = UIImageView(image:UIImage(named: imageName))
            // 添加到背景视图
            leftScoreBgV.addSubview(imageV)
        }
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
    }
    
    override func viewDidAppear(_ animated: Bool) {
        // 计算平均间距
//        let magin = leftScoreBgV.frame.width / 5
        // 获得背景视图的高度
//        let bottomY = leftScoreBgV.frame.height
//        
//        for (i,v) in leftScoreBgV.subviews.enumerated() {
//            // 设置图片视图的中心
//            v.center = CGPoint(x: magin/2 + CGFloat(i) * magin, y: bottomY - v.frame.height/2)
//        }
        
        // 计算平均间距
        let magin = leftScoreBgV.frame.width / 5
        
        for (i,v) in leftScoreBgV.subviews.enumerated() {
            
            let offsetM = magin * CGFloat(i) + magin/2
            
            v.snp.makeConstraints({ (make) in
                make.centerX.equalTo(leftScoreBgV.snp.left).offset(offsetM)
                make.bottom.equalTo(leftScoreBgV.snp.bottom).offset(-5)
            })
        }
    }
}









